package org.balabala.user.service.impl;

import com.alibaba.fastjson.TypeReference;
import org.balabala.user.entity.UserEntity;
import org.balabala.user.feign.VideoFeign;
import org.balabala.user.utils.Query;
import org.balabala.user.dao.VideoDao;
import org.balabala.user.entity.VideoEntity;
import org.balabala.user.service.VideoService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Resp;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("videoService")
public class VideoServiceImpl extends ServiceImpl<VideoDao, VideoEntity> implements VideoService {

    @Resource
    private  VideoDao videoDao;

    @Resource
    private VideoFeign videoFeign;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<VideoEntity> page = this.page(
                new Query<VideoEntity>().getPage(params),
                new QueryWrapper<VideoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils listByKey(HashMap<String,Object> hashMap) {
        String keyword = (String)hashMap.get("keyword");
        Integer state = (Integer)hashMap.get("state");
        if(keyword.equals("")){
            try {
                IPage<VideoEntity> page = this.page(
                        new Query<VideoEntity>().getPage(hashMap),
                        new QueryWrapper<VideoEntity>().eq("state",state)
                );
                List<VideoEntity> records = page.getRecords();
                for (VideoEntity record : records) {
                    Resp frameUrl = videoFeign.getVideoURL(record.getVideoId());
                    String url = frameUrl.getData("url",new TypeReference<String>() {
                    });
                    record.setVideoUrl(url);
                }
                page.setRecords(records);
                return new PageUtils(page);
            } catch (Exception e) {
                return null;
            }
        }else{
            try {
                //进行模糊查询
                IPage<VideoEntity> page = this.page(
                        new Query<VideoEntity>().getPage(hashMap),
                        new QueryWrapper<VideoEntity>()
                                .eq("state",state)
                                .and(item -> item.like("title",keyword).or().like("description",keyword))
                );
                List<VideoEntity> records = page.getRecords();
                for (VideoEntity record : records) {
                    Resp frameUrl = videoFeign.getVideoURL(record.getVideoId());
                    String url = frameUrl.getData(new TypeReference<String>() {
                    });
                    record.setVideoUrl(url);
                }
                page.setRecords(records);
                return new PageUtils(page);
            }catch (Exception e){
                return null;
            }

        }

    }

    @Override
    public boolean banById(Integer id, Integer state) {
        VideoEntity videoEntity = new VideoEntity();
        videoEntity.setId(id);
        videoEntity.setState(state);

        try {
            this.baseMapper.update(videoEntity,new QueryWrapper<VideoEntity>().eq("id",id));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

//    @Override
//    public boolean banByIds(Long[] ids, Integer state) {
//        Boolean bool = true;
//        for (Long integer : ids) {
//            try {
//                VideoEntity videoEntity = new VideoEntity();
//                int id = integer.intValue();
//                videoEntity.setId(id);
//                videoEntity.setState(state);
//                this.baseMapper.update(videoEntity,new QueryWrapper<VideoEntity>().eq("id",integer));
//            }catch (Exception e){
//                bool = false;
//            }
//        }
//        return bool;
//    }

}